<?php
/**
 * Created by PhpStorm.
 * User: 李闯
 * Date: 2020/5/22
 * Time: 10:29
 * Content: 登录
 */

namespace app\index\controller;

use Redis\Redis;
use think\Controller;
use think\Db;
use think\Request;

class BizLogin extends Controller
{

    function demo()
    {
        dump(explode(',', null));
    }

    /**
     * @return array
     * @content 申请门店注册
     * 姓名：biz_title
     * 联系方式:biz_phone
     * 门店地址:biz_address
     * 合作形式: 2门店加盟 3 门店合作 biz_type
     * 从业经验: 1无 2.1-3年 3. 3年以上 biz_experience
     * 门店照片:biz_permit
     */
    function BizRegister()
    {
        #获取门店注册所有信息
        $info = input("post.");
        $info['biz_create'] = date("Y-m-d H:i:s");
        $array = array();
        if (!empty($info)) {
            # 判断手机号
            $bizInfo = Db::table('biz')->field("biz_status")->where(array('biz_phone' => $info['biz_phone']))->find();
            if (!empty($bizInfo)) {
                if ($bizInfo['biz_status'] == 0) {
                    return array('status' => false, 'msg' => '该手机号已停用');
                } elseif ($bizInfo['biz_status'] == 1) {
                    return array('status' => false, 'msg' => '该手机号已被注册');
                } else {
                    return array('status' => false, 'msg' => '正在审核中');
                }
            }
            if (!empty($info['biz_permit'])) {
                foreach ($info['biz_permit'] as $v) {
//                    dump($v);
                    $imgUrl = uploadBase($v);
                    if (is_array($imgUrl)) {
                        return $imgUrl;
                    }
                    array_push($array, $imgUrl);
                }
                if (!empty($array)) {
                    $info['biz_permit'] = implode(',', $array);
                }
            }
            #插入数据
            $biz = Db::table('biz')->insert($info);
            if ($biz) {
                return array('status' => true, '提交成功,请等待审核!');
            } else {
                return array('status' => false, 'msg' => '提交失败');
            }
        } else {
            return array('status' => false, 'msg' => '申请信息为空');

        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 商户登录
     * phone=>手机号
     * code=>验证码
     */
        function bizlogin()
        {
            $phone = input("post.phone");
        $code = input("post.code");
        $redis = new Redis();
        # 设备id
        $cid = input('post.cid') ?? '';
        //测试号登陆  不需要验证码 直接登录 手机号自己写 (库中自己设置)短信验证码 123456
        if (!empty($phone) and !empty($code)) {
            if ($code == '2004') {
//                $redis->flush();
                goto login;
                #查询合作商户信息(type= 1) 返回member_id ,member_type 如果是员工登录 返回staff_type (员工类型)
//                $biz = Db::table('biz')->field('id coo_id,biz_address,biz_phone,biz_status,biz_type,biz_experience')->where(array('biz_phone' => $phone, 'biz_status' => 1))->where('biz_status = 1')->find();
//                return array('status' => true, 'data' => $biz);
            } else {
                #获取缓存验证码 跟$code 作对比
                $codeNum = $redis->hGet('SmsCode', $phone);
//                $redis->flush();
                if ($codeNum == $code) {
                    login:
                    #查询合作商户信息(type= 1)  如果是员工登录 返回staff_type (员工类型)
                    $biz = Db::table('biz')->field('*,id coo_id')->where(array('biz_phone' => $phone,'biz_status'=>1))->find();
                    if (!empty($biz)) {
//                        if ($biz['biz_type'] != 3) {
//                            return array('status' => false, 'msg' => '只为合作门店提供服务');
//                        }
                        #判断合作商户  只能有一个登录
//                        $redis_memberId = $redis->hGet('redis', "redis_bizId" . $biz['coo_id']);
//                        if ($biz['coo_id'] == $redis_memberId) {
//                            return array('status' => false, 'msg' => '此账号已登录,请退出后再进!');
//                        } else {
                        if ($biz['biz_status'] == 1) {
                            #存redis
//                                $redis->hSet('redis', "redis_bizId" . $biz['coo_id'], $biz['coo_id']);
                            # 老板登录
                            $biz['biz_head'] = imgUrl($biz['biz_img']);
                            # 客服电话
                            $bizInfo['service_tel'] = '400-8045288';
                            # 查询门店标签
                            $bizInfo['biz_tags'] = Db::table('service_class sc')
                                ->field('group_concat(distinct service_class_title) biz_tags')
                                ->where("id in (select service_class_id from service where id in (select service_id from service_biz where biz_id = " . $biz['coo_id'] . "))")
                                ->where("service_class_pid = 0")
                                ->find()['biz_tags'];
                            if (!empty($cid)) {
                                Db::table('biz')->where(array('id' => $biz['coo_id']))->update(array('cid' => $cid));
                            }
                            return array('status' => true, 'data' => $biz, 'loginType' => 1);
                        } else {
                            return array('status' => false, 'msg' => '正在审核中');
                        }
//                        }
                    } else {
                        # 判断是不是员工
                        $employee = Db::table('employee')->where(array('employee_phone' => $phone))->where("employee_status != 0")->find();
                        if (!empty($employee)) {
                            # 判断门店类型
//                            $bizType = Db::table('biz')->field('biz_type')->where(array('id' => $employee['biz_id']))->find();
//                            if (empty($bizType) or $bizType['biz_type'] != 3) {
//                                return array('status' => false, 'msg' => '只为合作门店提供服务');
//                            }
//                            # 判断员工登录 , 只能有一个
//                            $redis_employee = $redis->hGetJson('redis', 'redis_employee' . $employee['biz_id']);
//                            if (!empty($redis_employee) and in_array($employee['id'], $redis_employee)) {
//                                return array('status' => false, 'msg' => '此账号已登录,请退出后再进!');
//                            }
//                            if (empty($redis_employee)) {
//                                $redis_employee = array();
//                            }
//                            array_push($redis_employee, $employee['id']);
//                            #存redis
//                            $redis->hSetJson('redis', "redis_employee" . $employee['biz_id'], $redis_employee);
                            # 判断门店状态
                            $bizStatus = Db::table('biz')->field('biz_status')->where(array('id'=>$employee['biz_id']))->find();
                            if(!empty($bizStatus) and $bizStatus['biz_status']==1) {
                                # 员工登录
                                $employee['employee_head'] = imgUrl($employee['employee_head']);
//                            $employee['jobs_info'] = Db::table('jobs')->where(array('id' => $employee['jobs_id']))->find()['jobs_title'];
                                if (!empty($cid)) {
                                    Db::table('employee')->where(array('id' => $employee['id']))->update(array('cid' => $cid));
                                }
                                return array('status' => true, 'data' => $employee, 'loginType' => 2);
                            }else{
                                return array('status' => false, 'msg' => '该门店不存在或已下架');
                            }
                        } else {
                            return array('status' => false, 'msg' => '您不是门店员工或已离职');
                        }
                    }
                } else {
                    return array('status' => false, 'msg' => '验证码不正确!');
                }
            }
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 发送短信验证码
     * phone 手机号
     * mark 操作标识  loginPhone=>登录
     */
    function getSmsCode()
    {
        $phone = input("post.phone");
        # 接收操作标识
        $mark = input('post.mark');
        $bizId = \think\facade\Request::header('cooId');
        #判断下手机号是否存在
        $resPhone = Db::table('biz')->field('id,biz_status')->where(" biz_phone = $phone and biz_status in (1)")->find();
        if(!empty($resPhone)){
            $bizId = $resPhone['id'];
        }
        if ($mark == 'loginPhone') {
            # 登录发送验证码
            if (empty($resPhone)) {
                #查询员工表中的手机号
                $employeePhone = Db::table('employee')->field('id,biz_id')->where(array('employee_phone' => $phone))->where("employee_status != 0")->find();
                if (empty($employeePhone)) {
                    return array('status' => false, 'msg' => '该手机号未注册!');
                }
                $bizId = $employeePhone['biz_id'];
            } else if ($resPhone['biz_status'] == 2) {
                return array('status' => false, 'msg' => '该手机号正在审核中!');

            }
        }
        if ($mark == 'writePhone') {
            if (!empty($resPhone)) {
                return array('status' => false, 'msg' => '该手机号已被注册,请更换手机号');
            }
        }
        $sms = new SmsCode();
        #把验证码存一下redis
        $redis = new Redis();
        # 从redis中获取改手机号是否存在验证码
        $codeNum = $redis->hGet('SmsCode', $phone);
        if ($codeNum) {
            # 存在验证码
            $code = $sms->Ysms($phone, $codeNum, true, $bizId);
            if (!empty($code) and $code['status'] == true) {
                return array('status' => true, 'num' => $code['code'], 'msg' => '验证码发送成功');
            } else {
                return array('status' => false, 'msg' => '验证码发送失败');
            }
        } else {
            $code = $sms->Ysms($phone, randCodes(), true, $bizId);
            if (!empty($code) and $code['status'] == true) {
                $redis->hSet('SmsCode', $phone, $code['code'], '1800');
                return array('status' => true, 'num' => $code['code'], 'msg' => '验证码发送成功');
            } else {
                return array('status' => false, 'msg' => '验证码发送失败');
            }
        }

    }
}
